package yxy.game.pm2.bean.db.newDao;

import yxy.apple.db.DbManager;
import yxy.game.pm2.bean.db.po.PrivilegePo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author James
 * @date 2022/1/24 16:32
 **/
public class PrivilegeDAO {
    private static final String
            REPLACE_ONE = "replace into `privilege` (`pCode`, `type`, `endTime`, `updateTime`) values (?,?,?,?)",
            SELECT_ALL = "select * from `privilege` where `pCode` = ?",
            DELETE = "delete from `privilege` where `pCode` = ? and `updateTime` != ?";

    public static List<PrivilegePo> getList(String playerCode) {
        return DbManager.instance().executeQueryObjectList(SELECT_ALL, new Object[]{playerCode}, PrivilegePo.builder);
    }

    public static boolean replace(List<PrivilegePo> privilegePoList) {
        List<Object[]> list = new ArrayList<>();
        for (PrivilegePo privilegePo : privilegePoList) {
            Object[] objects = new Object[]{privilegePo.getPlayerCode(), privilegePo.getType(), privilegePo.getEndTime(), privilegePo.getUpdateTime()};
            list.add(objects);
        }
        int[] ints = DbManager.instance().executeBatchCommand(REPLACE_ONE, list);
        return ints.length == privilegePoList.size();
    }

    public static void delete(String pCode, long updateTime) {
        DbManager.instance().executeCommand(DELETE, new Object[]{pCode, updateTime});
    }


    public static PrivilegePo findOne(String playerCode, int privilegeid) {
        String sql = "select * from `privilege` where `pCode` = ? and `type` = ? ";
        List<PrivilegePo> list = DbManager.instance().executeQueryObjectList(sql, new Object[]{playerCode, privilegeid}, PrivilegePo.builder);
        return (list != null && list.size() > 0) ? list.get(0) : null;
    }

}
